using Godot;
using System;

public partial class GameManager : Node
{
    public static GameManager Instance { get; private set; }
    
    [Export]
    public int PlayerHealth { get; set; } = 100;
    
    [Export]
    public int PlayerScore { get; set; } = 0;
    
    [Export]
    public string CurrentLevel { get; set; } = "Level1";
    
    public event Action<int> OnHealthChanged;
    public event Action<int> OnScoreChanged;
    public event Action<string> OnLevelChanged;
    
    public override void _Ready()
    {
        // 单例模式
        if (Instance == null)
        {
            Instance = this;
            ProcessMode = ProcessModeEnum.Always;
        }
        else
        {
            QueueFree();
        }
    }
    
    public void ChangeHealth(int newHealth)
    {
        PlayerHealth = Mathf.Clamp(newHealth, 0, 100);
        OnHealthChanged?.Invoke(PlayerHealth);
        
        if (PlayerHealth <= 0)
        {
            GameOver();
        }
    }
    
    public void AddScore(int points)
    {
        PlayerScore += points;
        OnScoreChanged?.Invoke(PlayerScore);
    }
    
    public void ChangeLevel(string levelName)
    {
        CurrentLevel = levelName;
        OnLevelChanged?.Invoke(CurrentLevel);
        
        // 这里可以添加场景切换逻辑
        GD.Print($"切换到关卡: {levelName}");
    }
    
    private void GameOver()
    {
        GD.Print("游戏结束！");
        // 这里可以添加游戏结束逻辑
    }
    
    public void RestartGame()
    {
        PlayerHealth = 100;
        PlayerScore = 0;
        CurrentLevel = "Level1";
        
        OnHealthChanged?.Invoke(PlayerHealth);
        OnScoreChanged?.Invoke(PlayerScore);
        OnLevelChanged?.Invoke(CurrentLevel);
    }
} 